package branice.spring.cloud.controller;

import branice.spring.cloud.constants.GlobalConstants;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RefreshScope
@RequestMapping("/nacosClient")
@RestController
public class NacosClientController {

    private static final Logger log = LoggerFactory.getLogger(NacosClientController.class);

    @Value("${user.name:}")
    String name;

    /**
     * 验证nacos服务发现能力
     * 接收nacos-server发送的请求
     *
     * @param body
     * @param request
     * @return
     */
    @PostMapping("/reciveReq")
    public JSONObject receiveReq(@RequestBody String body, HttpServletRequest request){

        String logId = request.getHeader(GlobalConstants.X_ID);
        log.info("receiveReq-{}-req-执行开始, body={}", logId, body);

        JSONObject result = new JSONObject();
        result.put("name", name);
        log.info("receiveReq-{}-rep-执行完成, body={}", logId, body);
        return result;
    }



}
